package com.changgou.goods.controller;

import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.service.AlbumService;
import com.changgou.pojo.Album;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author ：Raoness
 * @date ：Created in 2019/7/23 18:14
 * @description ：
 * @version: 1.0
 */
@RestController
@RequestMapping("album")
public class AlbumController {

    @Autowired
    private AlbumService albumService;

    /**
     * 查找所有
     * @return
     */
    @GetMapping
    public Result findAll() {
        List<Album> albumList = albumService.findAll();
        return new Result(true, StatusCode.OK, "查询成功", albumList);
    }

    /**
     * 根据Id查找
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Result findById(@PathVariable Integer id) {
        Album album = albumService.findById(id);
        return new Result(true, StatusCode.OK, "查询成功", album);
    }

    /**
     * 添加
     * @param album
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Album album){
        albumService.add(album);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /**
     * 修改
     * @param album
     * @param id
     * @return
     */
    @PutMapping("{id}")
    public Result update(@RequestBody Album album,@PathVariable Integer id) {
        album.setId(id);
        albumService.update(album);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public Result delete(@PathVariable Integer id){
        albumService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /**
     * 条件查询
     * @param searchMap
     * @return
     */
    @GetMapping("search")
    public Result findList(@RequestParam Map<String, Object> searchMap) {
        List<Album> albumList = albumService.findList(searchMap);
        return new Result(true, StatusCode.OK, "查询成功", albumList);
    }

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @GetMapping("page/{page}/{size}")
    public Result findPage(@PathVariable int page, @PathVariable int size) {
        Page<Album> albums = albumService.findPage(page, size);
        PageResult<Album> pageResult = new PageResult<>(albums.getTotal(), albums.getResult());
        return new Result(true, StatusCode.OK, "查询成功", pageResult);
    }

    @GetMapping("search/{page}/{size}")
    public Result findPage(@PathVariable int page, @PathVariable int size, @RequestParam Map<String, Object> searchMap) {
        Page<Album> albums = albumService.findPage(searchMap, page, size);
        PageResult<Album> pageResult = new PageResult<>(albums.getTotal(), albums.getResult());
        return new Result(true, StatusCode.ACCESSERROR, "查询成功", pageResult);
    }
}
